﻿var Graffitis = Class.create();
Graffitis.prototype = {
	asyncHandle: null,
	loaded: [],
	templateName: '',

	asyncFuncs: {
		loading: function () {
			$("#loading").show();
		},
		loaded: function () {
			$("#loading").hide();
		},
		error: function (xhr, status) {
			alert("[" + status + "]" + xhr.responseText);
		}
	},

	_rootPath: '',

	init: function (rootPath, sessionId) {
		var self = this;
		this._rootPath = rootPath;
		// template name
		this.templateName = sessionId != 0 ? "#tfull" : "#tview";

		// delete command
		$("a.thumbnail_delete").live("click", function (e) {
			e.preventDefault();

			self.asyncFuncs.loading();
			var sender = this;
			var delId = parseInt($(sender).attr("rel"));
			$.ajax({
				type: "delete",
				url: self._rootPath + "graffitis.svc/Graffitis(" + delId + ")",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function (d) {
					$(sender).parents("li").fadeOut();
				},
				error: self.asyncFuncs.error,
				complete: self.asyncFuncs.loaded
			});
		});

		// addnew command
		$(".addnew").click(function (e, element) {
			e.preventDefault();

			self.asyncFuncs.loading();
			var data = {
				Id: 0,
				IsFinished: false
			};
			$.ajax({
				type: "post",
				url: self._rootPath + "graffitis.svc/Graffitis",
				contentType: "application/json; charset=utf-8",
				data: $.toJSON(data),
				dataType: "json",
				success: function (d) {
					var data = { id: d.d.Id };
					$(self.templateName)
					        .render(data)
					        .appendTo("#thumbs")
					        .fadeIn();

					self.loaded.push(d.d.Id);
				},
				error: self.asyncFuncs.error,
				complete: self.asyncFuncs.loaded
			});
		});

		// 監視開始
		this.load();
		this.asyncHandle = setInterval(function () { self.load(); }, 1000);
	},

	alive: function (val, target) {
		for (var i = 0, l = target.length; i < l; i++) {
			if (val == target[i].Id)
				return true;
		}
		return false;
	},

	load: function () {
		var self = this;
		self.asyncFuncs.loading();
		if ($.active > 0)
			return;

		$.ajax({
			type: "get",
			url: self._rootPath + "graffitis.svc/Graffitis",
			cache: false,
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function (d) {
				// 追加
				var list = [];
				$(d.d).each(function (idx, val) {
					list.push(val.Id);
					if ($.inArray(val.Id, self.loaded) == -1) {
						$(self.templateName)
                                .render({ id: val.Id })
					            .appendTo("#thumbs")
					            .fadeIn();

						self.loaded.push(val.Id);
					}
				});
				// 削除
				var dels = [];
				$(self.loaded).each(function (idx, val) {
					var del = $.inArray(val, list);
					if (del == -1) {
						$("#graffiti_" + val).fadeOut().remove();
						dels.push(idx);
					}
				});

				$(dels).each(function (idx, val) {
					self.loaded.splice(val, 1);
				});
			},
			error: self.asyncFuncs.error,
			complete: self.asyncFuncs.loaded
		});

	}
}
